//
//  FoundViewController.m
//  Koala_Fm
//
//  Created by dllo on 16/1/16.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "FoundViewController.h"
#import "ArticleCollectionViewCell.h"
#import "AFNetBlock.h"
#import "SheerScrollView.h"
#import <UIImageView+WebCache.h>
#import "PictureModel.h"
#import "FoundTableViewCell.h"
#import "FoundModel.h"
#import "ReferTableViewCell.h"
#import "DetaLisViewController.h"
#import "EntertainViewController.h"
#import "ListenViewController.h"
#import "TopicsViewController.h"
#import "SearchViewController.h"
#import "HotViewController.h"
@interface FoundViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UITableViewDelegate, UITableViewDataSource>
#define W self.view.bounds.size.width/375
#define H self.view.bounds.size.height/667
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *dataArr;//上部CollectionView数据数组
@property(nonatomic, retain)SheerScrollView *scrollView;// 轮播
@property(nonatomic, retain)UIView *header;//头部区域
@property(nonatomic, retain)UICollectionView *collect;
@property(nonatomic, retain)NSMutableArray *array;//分区数组
@property(nonatomic, retain)NSString *string;
@end

@implementation FoundViewController

- (void)dealloc
{
    [_tableView release];
    [_dataArr release];
    [_scrollView release];
    [_header release];
    [_collect release];
    [_array release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    /* tableView设置 */
    [self tableHandler];
    /** 导航栏设置 **/
    [self navigationBar];
    /** 整页翻动 **/
    [self PageShuffling];
    /** 上部横条数据获取 **/
    [self detaHandler];
    /** 轮播数据获取 **/
    [self dataShuffling];
    /** 首页数据获取 **/
    [self dadadadad];
    /** 头部设置 **/
    [self lower];
    /** button设置 **/
    [self buttonbu];
    /** label 设置 **/
//    [self labella];
   }
#pragma mark - tableView设置
- (void)tableHandler{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 30, self.view.frame.size.width, self.view.frame.size.height - 100) style:UITableViewStylePlain];
//    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_tableView];
    [_tableView release];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    // 重用池
    [_tableView registerClass:[ReferTableViewCell class] forCellReuseIdentifier:@"tableCell"];
}

#pragma mark - 轮播数据获取
- (void)dataShuffling{
    NSString *arr = @"http://api.kaolafm.com/api/v4/pagecontent/list?pageid=1&installid=0002JX3E&udid=bd80d675b1bb04bd76de2a42a3aca09d&sessionid=bd80d675b1bb04bd76de2a42a3aca09d1452823374513&imsi=460077347218243&operator=0&lon=121.544758&lat=38.883353&network=1&timestamp=1452823557&sign=ad0e64ef4e4695f26aab782327753a6b&resolution=720*1280&devicetype=0&channel=B-xiaomi&version=4.5.1&appid=0&";
    [AFNetBlock GetDataWithURL:arr block:^(id block) {
        // 轮播数组获取
        NSArray *arrayTow = [block[@"result"][@"dataList"] firstObject][@"dataList"];

        self.dataArr = [PictureModel modelHandlerWithArray:arrayTow];

        // 创建轮播视图
        [self Seeding];

       // 刷新页面
        
        [_tableView reloadData];
    }];
}
#pragma mark - 数据获取
- (void)dadadadad{
    _string = @"http://api.kaolafm.com/api/v4/resource/search?words=&cid=664&sorttype=HOT_RANK_DESC&pagesize=10&pagenum=1&rtype=20000&installid=0002JX3E&udid=bd80d675b1bb04bd76de2a42a3aca09d&sessionid=bd80d675b1bb04bd76de2a42a3aca09d1452942299513&imsi=460077347218243&operator=0&lon=0.0&lat=0.0&network=1&timestamp=1452944693&playid=5ff397524b086b4e7b091b6acdeab02f&sign=ad0e64ef4e4695f26aab782327753a6b&resolution=720*1280&devicetype=0&channel=B-xiaomi&version=4.5.1&appid=0&";
    [AFNetBlock GetDataWithURL:_string block:^(id block) {
        self.array = block[@"result"][@"dataList"];
        
        // 刷新页面

        [_tableView reloadData];
    }];
}

#pragma mark - 轮播图
- (void)Seeding{
    self.scrollView = [[SheerScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)animationDuration:6];
    NSMutableArray *viewArr = [[NSMutableArray alloc]init];
    for (NSInteger i = 0; i < _dataArr.count; i++) {
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
       PictureModel *m = _dataArr[i];
        [image sd_setImageWithURL:[NSURL URLWithString:m.pic]];
        [viewArr addObject:image];
    }
    [_scrollView setFetchContentViewAtIndex:^UIView *(NSInteger(pageIndex)) {
        return [viewArr objectAtIndex:pageIndex];
    }];
    [_scrollView setTotalPagesCount:^NSInteger{
        return viewArr.count;
    }];
    [_tableView.tableHeaderView addSubview:_scrollView];
}

#pragma mark - 头部区域
- (void)lower{
    self.header = [[UIView alloc]initWithFrame:CGRectMake(0, 30, 0, 200)];
//        _header.backgroundColor = [UIColor grayColor];
    _tableView.tableHeaderView = _header;
//    _header.userInteractionEnabled = YES;
    
}
#pragma mark - button 设置
- (void)buttonbu{
    //段子按钮
//    UIButton *psButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    psButton.frame = CGRectMake(10, 210, 60*W, 60);
//    psButton.backgroundColor = [UIColor redColor];
//    [_header addSubview:psButton];
//    [psButton setBackgroundImage:[UIImage imageNamed:@"dz@2x.jpg"] forState:UIControlStateNormal];
//    psButton.layer.cornerRadius = 30.0;
//    psButton.layer.borderWidth = 1.0;
//    psButton.layer.borderColor = [UIColor clearColor].CGColor;
//    psButton.clipsToBounds = TRUE;
//    [psButton addTarget:self action:@selector(psAction) forControlEvents:UIControlEventTouchUpInside];
    //图片按钮
//    UIButton *dzButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    dzButton.frame = CGRectMake(100, 210, 60*W, 60);
//    dzButton.backgroundColor = [UIColor redColor];
//    [_header addSubview:dzButton];
//    [dzButton setBackgroundImage:[UIImage imageNamed:@"tpp@2x.jpg"] forState:UIControlStateNormal];
//    dzButton.layer.cornerRadius = 30.0;
//    dzButton.layer.borderWidth = 1.0;
//    dzButton.layer.borderColor = [UIColor clearColor].CGColor;
//    dzButton.clipsToBounds = TRUE;
//    [psButton addTarget:self action:@selector(dzAction) forControlEvents:UIControlEventTouchUpInside];
    //相声按钮
//    UIButton *xsButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    xsButton.frame = CGRectMake(190, 210, 60*W, 60);
//    xsButton.backgroundColor = [UIColor redColor];
//    [_header addSubview:xsButton];
//    [xsButton setBackgroundImage:[UIImage imageNamed:@"xs@2x.jpg"] forState:UIControlStateNormal];
//    xsButton.layer.cornerRadius = 30.0;
//    xsButton.layer.borderWidth = 1.0;
//    xsButton.layer.borderColor = [UIColor clearColor].CGColor;
//    xsButton.clipsToBounds = TRUE;
//    [psButton addTarget:self action:@selector(xsAction) forControlEvents:UIControlEventTouchUpInside];
    //视屏按钮
//    UIButton *spButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    spButton.frame = CGRectMake(280, 210, 60*W, 60);
//    spButton.backgroundColor = [UIColor redColor];
//    [_header addSubview:spButton];
//    [spButton setBackgroundImage:[UIImage imageNamed:@"spp@2x.jpg"] forState:UIControlStateNormal];
//    spButton.layer.cornerRadius = 30.0;
//    spButton.layer.borderWidth = 1.0;
//    spButton.layer.borderColor = [UIColor clearColor].CGColor;
//    spButton.clipsToBounds = TRUE;
//    [spButton addTarget:self action:@selector(spAction) forControlEvents:UIControlEventTouchUpInside];
}
// button 按钮实现方法
- (void)psAction{
    NSLog(@"5654646546");
}
- (void)dzAction{
    
}
- (void)xsAction{
    
}
- (void)spAction{
    
}
- (void)labella{
    //
    UILabel *passLable = [[UILabel alloc]initWithFrame:CGRectMake(20, 280, 60*W, 20)];
//   passLable.backgroundColor = [UIColor grayColor];
     passLable.text = @"热门段子";
    [_header addSubview:passLable];
    [passLable release];
    //
    UILabel *dzLable = [[UILabel alloc]initWithFrame:CGRectMake(100, 280, 60*W, 20)];
    dzLable.text = @"搞笑图片";
//    dzLable.backgroundColor = [UIColor grayColor];
    [_header addSubview:dzLable];
    [dzLable release];
    //
    UILabel *xsLable = [[UILabel alloc]initWithFrame:CGRectMake(190, 280, 60*W, 20)];
     xsLable.text = @"相声集合";
//    xsLable.backgroundColor = [UIColor grayColor];
    [_header addSubview:xsLable];
    [xsLable release];
    //
    UILabel *spLable = [[UILabel alloc]initWithFrame:CGRectMake(280, 280, 60*W, 20)];
    spLable.text = @"爆笑视频";
//    spLable.backgroundColor = [UIColor grayColor];
    [_header addSubview:spLable];
    [spLable release];
}

#pragma mark - 数据获取
- (void)detaHandler{
    self.dataArr = [@[@"推荐", @"分类",@"专题",@"娱乐",@"榜单"]mutableCopy];
    [_collect reloadData];
}
#pragma mark - 导航栏设置
- (void)navigationBar{
    // 设置导航栏为不透明
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"首页";
    /** 右按钮 **/
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"yl"] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction)];
    /** 左按钮 **/
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"sss"] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction)];

}
// 右按钮点击方法
- (void)rightAction{
    
}
#pragma mark - 左按钮
- (void)leftAction{
    SearchViewController *search = [[SearchViewController alloc]init];
    
    // 传值给下一页
    search.string = _string ;
    [self.navigationController pushViewController:search animated:YES];
    [search release];
}


#pragma mark - 上部CollectionView
- (void)PageShuffling{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    // 横向滚动
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    // CollectionView 大小
    layout.itemSize = CGSizeMake(self.view.frame.size.width / 5,30);
    layout.minimumLineSpacing =0;
    self.collect = [[UICollectionView alloc]initWithFrame:CGRectMake(10, 0, self.view.frame.size.width, 30) collectionViewLayout:layout];
    _collect.backgroundColor = [UIColor whiteColor];
    _collect.pagingEnabled = YES;
    _collect.delegate = self;
    _collect.dataSource = self;
    [self.view addSubview:_collect];
    // 重用池
    [_collect registerClass:[ArticleCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}
#pragma mark - CollectionView设
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
       return _dataArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
        ArticleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        
        cell.lable.text =_dataArr[indexPath.row];
        
       return cell;
    }


#pragma mark - tableView设置
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ReferTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableCell" forIndexPath:indexPath];
    
    [cell.imag sd_setImageWithURL:[NSURL URLWithString:_array[indexPath.row][@"pic"]]];
    cell.imagOne.image =[UIImage imageNamed:@"jiahao"];
    cell.imagTow.image =[UIImage imageNamed:@"erji"];
    cell.titleLable.text = _array[indexPath.row][@"name"];
    cell.contentLable.text = _array[indexPath.row]
    [@"desc"];
    // 判断数字是否超一万
    if ([_array[indexPath.row][@"followedNum"] floatValue] > 10000) {
        
        NSString *str = [NSString stringWithFormat:@"%.1f万次订阅",[_array[indexPath.row][@"followedNum"] floatValue] / 10000];
        cell.subscribeLable.text = str;
    }else{
        NSString *ster = [NSString stringWithFormat:@"%.1f次订阅",[_array[indexPath.row][@"followedNum"] floatValue]];
        cell.subscribeLable.text = ster;
    }
    if ([_array[indexPath.row][@"listenNum"]floatValue] > 10000) {
        NSString *strr = [NSString stringWithFormat:@"%.1f万次收听",[_array[indexPath.row][@"listenNum"]floatValue] / 10000];
        cell.listenLable.text = strr;
        
    }

        return cell;
}
 // cell 高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

// 点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DetaLisViewController *deta = [[DetaLisViewController alloc]init];
    //传值到下一页
    NSString *name = [NSString stringWithFormat:@"%@",_array[indexPath.row][@"name"]];
    NSString *strry = [NSString stringWithFormat:@"%@",_array[indexPath.row][@"id"] ];
    NSString *iamge = [NSString  stringWithFormat:@"%@",_array[indexPath.row][@"pic"] ];
    deta.str =strry;
    deta.name = name;
    deta.image =iamge;
    [self.navigationController pushViewController:deta animated:YES];
    
}
// 点击coll
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
   
    if (indexPath.item == 0) {
        ListenViewController *listen = [[ListenViewController alloc]init];
        [self.navigationController pushViewController:listen animated:YES];
    } else if(indexPath.item == 1){
        
        
    } else if(indexPath.item == 2){
        TopicsViewController *topics = [[TopicsViewController alloc]init];
        [self.navigationController pushViewController:topics animated:YES];
        
        
        
    } else if(indexPath.item == 3){
        EntertainViewController *entertain = [[EntertainViewController alloc]init];
        [self.navigationController pushViewController:entertain animated:YES];
        
    } else{
        HotViewController *hot = [[HotViewController alloc]init];
        [self.navigationController pushViewController:hot animated:YES];
        
    }
}
        
#pragma mark - 引导页
- (void)viewWillAppear:(BOOL)animated{
    // 引导图通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"addLeadView" object:nil userInfo:nil];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
